/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

export const TASK_EXECUTE_STATUS = {
  BLOCKED: 0,
  PENDING: 1,
  RUNNING: 2,
  END: 3,
  CANCELLED: 4,
  FAILED: 5,
};

/**
 * 用于保存一些任务执行状态的信息
 *
 * @since 2022/9/1
 */
export class TaskExecuteStatus {
  private _state: number;
  private _taskBeginTime: [number, number] | undefined;
  private _workerTimePeriod: [number, number][] = [];
  private _isUpToDate = false;
  public unTrackStateReason: string | undefined;

  constructor() {
    this._state = TASK_EXECUTE_STATUS.BLOCKED;
  }

  public setState(state: number) {
    if (state < this._state) {
      throw new Error('Cannot change the task status.');
    }
    this._state = state;

    if (state === TASK_EXECUTE_STATUS.RUNNING) {
      this._taskBeginTime = process.hrtime();
    }
  }

  public getState(): number {
    return this._state;
  }

  public getTaskBeginTime(): [number, number] | undefined {
    return this._taskBeginTime;
  }

  public setIsUpToDate(upToDate: boolean): void {
    this._isUpToDate = upToDate;
  }

  public isUpToDate(): boolean {
    return this._isUpToDate;
  }

  public setWorkerTimePeriod(time: [number, number]) {
    this._workerTimePeriod.push(time);
  }

  public getWorkerTimePeriod() {
    return this._workerTimePeriod;
  }
}
